﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;

namespace Travianer.Common
{
    public static class Utility
    {
        /// <summary> 
        /// 根据枚举值获得枚举描述
        /// </summary> 
        /// <param name="enumType">枚举类型</param> 
        /// <param name="nValue">枚举值</param> 
        /// <returns></returns> 
        public static string GetTextByValue(this Type enumType, int nValue)
        {
            if (!enumType.IsEnum)
                throw new InvalidOperationException();

            string text = string.Empty;

            // 获取Description特性 
            Type typeDescription = typeof(DescriptionAttribute);
            // 获取枚举字段
            FieldInfo[] fields = enumType.GetFields();
            foreach (FieldInfo field in fields)
            {
                if (!field.FieldType.IsEnum)
                    continue;

                // 获取枚举值
                int value = (int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null);

                if (value == nValue)
                {
                    object[] array = field.GetCustomAttributes(typeDescription, false);

                    if (array.Length > 0) text = ((DescriptionAttribute)array[0]).Description;
                    else text = field.Name; //没有描述，直接取值
                }
            }
            return text;
        }
        /// <summary> 
        /// 根据枚举值获得枚举描述
        /// </summary> 
        /// <param name="enumType">枚举类型</param> 
        /// <param name="strValues">枚举值(多个值用","隔开)</param> 
        /// <returns></returns> 
        public static string GetTextByValue(this Type enumType, string strValues)
        {
            if (!enumType.IsEnum)
                throw new InvalidOperationException();

            strValues = "," + strValues + ",";
            // 获取Description特性 
            Type typeDescription = typeof(DescriptionAttribute);
            // 获取枚举字段
            FieldInfo[] fields = enumType.GetFields();
            foreach (FieldInfo field in fields)
            {
                if (!field.FieldType.IsEnum)
                    continue;

                // 获取枚举值
                int value = (int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null);

                if (value >= 0)
                {
                    string text = string.Empty;
                    object[] array = field.GetCustomAttributes(typeDescription, false);

                    if (array.Length > 0) text = ((DescriptionAttribute)array[0]).Description;
                    else text = field.Name; //没有描述，直接取值

                    strValues = strValues.Replace("," + value + ",", "," + text + ",");
                }
            }
            return strValues.Trim(',');
        }

        public static bool IsIn(this string str1, string str2)
        {
            return ("," + str2 + ",").IndexOf("," + str1 + ",") >= 0;
        }
    }
}
